package com.example.cat.catwidget.Utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.cat.catwidget.Server.MyApplication;

/**
 * Created by cat on 2018/4/30.
 */

public class SharedPreferencesHelper {
    /**
     * 写入内容，需要主键，字段，和值，模式为覆盖
     *
     * @param SharedPreferencesKey
     * @param SaveValueKey
     * @param SaveValue
     */
    public static void SaveValueForWrite(String SharedPreferencesKey, String SaveValueKey, String SaveValue) {
        SavaStringValue(SharedPreferencesKey, Context.MODE_PRIVATE, SaveValueKey, SaveValue);
    }

    public static void SaveIntValueForWrite(String SharedPreferencesKey, String SaveValueKey, int SaveValue) {
        SavaIntValue(SharedPreferencesKey, Context.MODE_PRIVATE, SaveValueKey, SaveValue);
    }

    public static int GetIntValue(String SharedPreferencesKey, String SaveValueKey) {

        SharedPreferences sp = MyApplication.getContext().getSharedPreferences(SharedPreferencesKey, Context.MODE_PRIVATE);

        //取出数据,第一个参数是写入是的键，第二个参数是如果没有获取到数据就默认返回的值。
        int value = sp.getInt(SaveValueKey, 0);
        return value;

    }

    public static void SavaIntValue(String SharedPreferencesKey, int SharedPreferenceMode, String SaveValueKey, int SaveValue) {

        SharedPreferences sp = MyApplication.getContext().getSharedPreferences(SharedPreferencesKey, SharedPreferenceMode);
        //获取到edit对象
        SharedPreferences.Editor edit = sp.edit();
        //通过editor对象写入数据
        edit.putInt(SaveValueKey, SaveValue);
        //提交数据存入到xml文件中
        edit.commit();

    }

    /**
     * 保存信息通用模式
     *
     * @param SharedPreferencesKey
     * @param SharedPreferenceMode
     * @param SaveValueKey
     * @param SaveValue
     */
    public static void SavaStringValue(String SharedPreferencesKey, int SharedPreferenceMode, String SaveValueKey, String SaveValue) {

        SharedPreferences sp = MyApplication.getContext().getSharedPreferences(SharedPreferencesKey, SharedPreferenceMode);
        //获取到edit对象
        SharedPreferences.Editor edit = sp.edit();
        //通过editor对象写入数据
        edit.putString(SaveValueKey, SaveValue);
        //提交数据存入到xml文件中
        edit.commit();

    }

    /**
     * 读取String类型的值，若为空则返回null
     *
     * @param SharedPreferencesKey
     * @param SaveValueKey
     * @return
     */
    public static String GetStringValue(String SharedPreferencesKey, String SaveValueKey) {

        SharedPreferences sp = MyApplication.getContext().getSharedPreferences(SharedPreferencesKey, Context.MODE_PRIVATE);

        //取出数据,第一个参数是写入是的键，第二个参数是如果没有获取到数据就默认返回的值。
        String value = sp.getString(SaveValueKey, "Null");

        return value;
    }
}
